package chat;
 

import com.*;
import client.UDPChatClient;
import java.util.List;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * 
 * @author Mathias
 */
public class ChatCommunication implements ICommunication {

    private Sender mySender;
    private UDPChatClient chatClient = null;

    public ChatCommunication(SocketConfig sc, UDPChatClient chatClient) throws java.net.SocketException{
        try{
            mySender = new Sender(sc, chatClient);
        } catch(java.net.SocketException e) {
            System.err.println("ChatCommunication Exception: " + e);
            throw e;
        }
        this.chatClient = chatClient;
    }

    public void setNickname(String nickname){
       ChatCommand command = new ChatCommand(CmdEnum.SETNICKNAME);
       System.out.println("[setNickName] " + nickname);
       command.setNickName(nickname);
        try{
            mySender.send(command);
        } catch (Exception e) {
            System.err.println("setNickName Exception: " + e);
        }
    }
    

    public void sendMsg(String msg, String senderName, List<String> recipients) throws Exception {
        ChatMessage message = new ChatMessage(msg, senderName, recipients);
        System.out.println("[sendMsg] " + msg);
        ChatCommand chatCmd = new ChatCommand(CmdEnum.SENDMSG);
        chatCmd.setChatMsg(message);
        chatCmd.setNickName(chatClient.getNickName());
        try{
            mySender.send(chatCmd);
        } catch (Exception e) {
            System.err.println("sendMsg Exception: " + e);
            throw e;
        }
    }

    public void poll() throws Exception {
        ChatCommand command = new ChatCommand(CmdEnum.POLL);
        command.setNickName(chatClient.getNickName());
        command.setMsgId(chatClient.getMsgId());
        //System.out.println("[poll] " + command.getMsgId());
        try{
            mySender.send(command);
        } catch (Exception e) {
            System.err.println("poll Exception: " + e);
            throw e;
        }
    }

    
}
